//////////////////////////////////////////////////////////////////////////
// FileName:SchSysVariable.aspx
// CreateBy:Mark
// CreateDate:2009-11-13
// Description:
//		Search and delete system variable 
//
// Modify Log:
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
namespace BaseSystem._System
{
    public partial class SchSysVariable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                init();
            }
        }

        private void init()
        {
            BindCookies();
            BindSession();
            BindApplication();
        }

        private void BindCookies()
        {
            DataTable dt;
            dt = MakeTable();
            DataRow dr;

            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                if (HttpContext.Current.Request.Cookies[i].Value != null)
                {
                    dr = dt.NewRow();
                    dr["key"] = HttpContext.Current.Request.Cookies.Keys[i].ToString();
                    dr["value"] = HttpContext.Current.Request.Cookies[i].Value.ToString();
                    dt.Rows.Add(dr);
                }
            }
            GVCookies.DataSource = dt;
            GVCookies.DataBind();
        }

        private void BindSession()
        {
            DataTable dt;
            dt = MakeTable();
            DataRow dr;
            foreach (string key in Session.Contents)
            {
                dr = dt.NewRow();
                dr["key"] = key.ToString();
                dr["value"] = Session[key].ToString();
                dt.Rows.Add(dr);
            }
            GVSession.DataSource = dt;
            GVSession.DataBind();
        }

        private void BindApplication()
        {
            DataTable dt;
            dt = MakeTable();
            DataRow dr;

            for (int i = 0; i < HttpContext.Current.Application.Count;i++)
            {
                Dictionary<string, string> d = (Dictionary<string, string>)(HttpContext.Current.Application[i]);
                string strva = "";
                foreach (string va in d.Keys)
                {
                    strva += va + "=" + d[va].ToString();
                }
                dr = dt.NewRow();
                dr["key"] = HttpContext.Current.Application.Keys[i].ToString();
                dr["value"] = strva;
                dt.Rows.Add(dr);
            }
            GVApplication.DataSource = dt;
            GVApplication.DataBind();
        }

        private DataTable MakeTable()
        {
            DataTable namesTable = new DataTable("Names");

            DataColumn fNameColumn = new DataColumn();
            fNameColumn.DataType = System.Type.GetType("System.String");
            fNameColumn.ColumnName = "key";
            namesTable.Columns.Add(fNameColumn);

            DataColumn lNameColumn = new DataColumn();
            lNameColumn.DataType = System.Type.GetType("System.String");
            lNameColumn.ColumnName = "value";
            namesTable.Columns.Add(lNameColumn);

            return namesTable;
        }

        protected void btnCookies_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Request.Cookies.Count; i++)
            {
                if (Response.Cookies[Request.Cookies[i].Name] != null)
                {
                    Response.Cookies[Request.Cookies[i].Name].Expires = DateTime.Now.AddHours(-1);
                }
            }
            BindCookies();
        }

        protected void btnSession_Click(object sender, EventArgs e)
        {
            //Session.Clear();
            Session.RemoveAll();
            BindSession();
        }

        protected void btnApplication_Click(object sender, EventArgs e)
        {
            //Application.Clear();
            Application.RemoveAll();
            BindApplication();
        }

    }
}
